package com.yc.storage.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.api.CommonPage;
import com.yc.api.CommonResult;
import com.yc.base.BaseController;
import com.yc.storage.entity.FileStorage;
import com.yc.storage.service.IFileStorageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 文件管理 前端控制器
 * </p>
 *
 * @author bao
 * @since 2022-04-10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/file-storage")
@Api(value = "FileStorageController", tags = "文件管理")
public class FileStorageController extends BaseController {

    private final IFileStorageService fileStorageService;

    @PostMapping("/add")
    @ApiOperation(value = "创建FileStorage",notes = "创建FileStorage")
    public CommonResult<?> create(@RequestBody FileStorage fileStorage) {
        fileStorage.setId(null);
        boolean isOk = fileStorageService.create(fileStorage);
        if (isOk) {
            return CommonResult.success(fileStorage);
        }
        return CommonResult.failed();
    }

    @PostMapping("/deleteByIds")
    @ApiOperation(value = "删除FileStorage",notes = "删除FileStorage")
    public CommonResult<?> delete(@RequestBody List<Long> ids) {
        boolean isOk = fileStorageService.delete(ids);
        if (isOk) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @PostMapping("/updateById/{id}")
    @ApiOperation(value = "更新FileStorage",notes = "更新FileStorage")
    public CommonResult<?> update(@PathVariable("id") Long id, @RequestBody JSONObject params) {
        FileStorage fileStorage = JSONUtil.toBean(params, FileStorage.class);
        fileStorage.setId(id);
        boolean isOk = fileStorageService.update(fileStorage);
        if (isOk) {
            return CommonResult.success(fileStorage);
        }
        return CommonResult.failed();
    }

    @PostMapping("/queryById/{id}")
    @ApiOperation(value = "根据Id获取FileStorage",notes = "根据Id获取FileStorage")
    public CommonResult<?> queryById(@PathVariable("id") Long id){
        FileStorage fileStorage = fileStorageService.queryById(id);
        if (fileStorage != null){
            return CommonResult.success(fileStorage);
        }
        return CommonResult.failed();
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取FileStorage列表",notes = "获取FileStorage列表")
    public CommonResult<?> listAll() {
        List<FileStorage> list = fileStorageService.selectList();
        return CommonResult.success(list);
    }

    @PostMapping("/page")
    @ApiOperation(value = "获取FileStorage列表",notes = "获取FileStorage列表")
    public CommonResult<?> pageAll(@RequestBody JSONObject params) {
        int pageNum = params.getInt("pageNum", 1);
        int pageSize = params.getInt("pageSize", 25);
        IPage<FileStorage> page = fileStorageService.selectPage(new Page<>(pageNum, pageSize));
        return CommonResult.success(CommonPage.restPage(page));
    }
}
